<?php
    // new cat
    class NewsCatModel
    {
        public function __construct(PDO $pdo) {
            $this->pdo = $pdo;
        }
        public function getAllCatNews()
        {
            $sql = "SELECT * FROM news_cat";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;            
        }
        function getNameCatByID($catID)
        {
            $sql = "SELECT * FROM news_cat where IDCAT_NEWS = $catID LIMIT 1";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetch();
            return $result;
        }
        function getCatNewsOther($catID)
        {
            $sql = "SELECT * FROM news_cat where IDCAT_NEWS <> $catID";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }                
        public function insertCatNews($arrayCat)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayCat['NAMECAT_NEWS'])));
            $sql = "INSERT INTO news_cat (
                IDPARENT_NEWS,
                NAMECAT_NEWS,
                NAMESEOCAT_NEWS,
               	DES_CATNEWS,
                SUMARYSEO_CAT,
                URLCAT_NEWS,
                STATUSCAT_NEWS
                ) 
                VALUES (
                :IDPARENT_NEWS,
                :NAMECAT_NEWS,
                :NAMESEOCAT_NEWS,
                :DES_CATNEWS,
                :SUMARYSEO_CAT,
                :URLCAT_NEWS,
                :STATUSCAT_NEWS
                )";
            $sth = $this->pdo->prepare($sql);
            $result = $sth->execute(array(
                                ':IDPARENT_NEWS'        =>$arrayCat['IDPARENT_NEWS'],
                                ':NAMECAT_NEWS'         =>$arrayCat['NAMECAT_NEWS'],
                                ':NAMESEOCAT_NEWS'      =>$arrayCat['NAMESEOCAT_NEWS'],
                                ':DES_CATNEWS'          =>$arrayCat['DES_CATNEWS'],
                                ':SUMARYSEO_CAT'        =>$arrayCat['SUMARYSEO_CAT'],
                                ':URLCAT_NEWS'          =>$nameUrlSeo,
                                ':STATUSCAT_NEWS'       =>isset($arrayCat['STATUSCAT_NEWS'])?1:0,                         
                        ));
                            
            return true;            
        }
        function editCatNews($arrayCat)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayCat['NAMECAT_NEWS'])));
            $sql = "UPDATE news_cat 
                    SET IDPARENT_NEWS=?, NAMECAT_NEWS=?, NAMESEOCAT_NEWS=?, 
                    DES_CATNEWS=?, SUMARYSEO_CAT=?,URLCAT_NEWS=?,STATUSCAT_NEWS=?
            		WHERE IDCAT_NEWS=?";
            $sth = $this->pdo->prepare($sql);
            $result = $sth->execute(array($arrayCat['IDPARENT_NEWS'],$arrayCat['NAMECAT_NEWS'],$arrayCat['NAMESEOCAT_NEWS'],
            $arrayCat['DES_CATNEWS'],$arrayCat['SUMARYSEO_CAT'],$nameUrlSeo,isset($arrayCat['STATUSCAT_NEWS'])?1:0,$arrayCat['IDCAT_NEWS']));               
            return true;
        }
        function checkAllChildCat($ID_CAT){
            $sql = "SELECT ID_CAT news_cat WHERE IDPARENT_NEWS=$ID_CAT ";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }
        function checkAllNewsCat($ID_CAT){
            $sql = "SELECT ID_PRODUCT news_cat WHERE IDCAT_NEWS=$ID_CAT ";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }
        function deleteCat($ID_CAT)
        {
            $sql = "DELETE FROM `news_cat` WHERE IDCAT_NEWS=$ID_CAT ";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();            
        }                         
    }
